package com.im.partchat.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.im.partchat.R;
import com.im.partchat.adapter.learn.NewsTabAdapter;
import com.im.partchat.base.BaseFragment;
import com.im.partchat.bean.eventbus.NagvationToLearn;
import com.im.partchat.ui.fragment.learn.Tab1Fragment;
import com.im.partchat.ui.fragment.learn.Tab2Fragment;
import com.im.partchat.ui.fragment.learn.Tab3Fragment;
import com.im.partchat.ui.fragment.learn.Tab4Fragment;
import com.im.partchat.ui.fragment.learn.Tab5Fragment;
import com.im.partchat.ui.fragment.learn.Tab6Fragment;
import com.im.partchat.ui.fragment.learn.Tab7Fragment;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by fzl on 2017/6/26 0026.
 * VersionCode: 1
 * Desc:
 */

public class LearnFragment extends BaseFragment implements ViewPager.OnPageChangeListener {
    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tab_news_title)
    TabLayout tabNewsTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.vp_news_pager)
    ViewPager vpNewsPager;

    private NewsTabAdapter newsTabAdapter;
    private List<Fragment> list_fragment;
    private List<String> list_title;

    public LearnFragment() {
    }


    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        list_fragment = new ArrayList<>();
        list_fragment.add(new Tab1Fragment());
        list_fragment.add(new Tab2Fragment());
        list_fragment.add(new Tab3Fragment());
        list_fragment.add(new Tab4Fragment());
        list_fragment.add(new Tab5Fragment());
        list_fragment.add(new Tab6Fragment());
        list_fragment.add(new Tab7Fragment());
        list_title = new ArrayList<>();
        list_title.add("新闻动态");
        list_title.add("两学一做");
        list_title.add("知识问答");
        list_title.add("微党课");
        list_title.add("典型案例");
        list_title.add("企业风采");
        list_title.add("公众平台");

        //设置TabLayout的模式
        tabNewsTitle.setTabMode(TabLayout.MODE_SCROLLABLE);

        tabNewsTitle.addTab(tabNewsTitle.newTab().setText(list_title.get(0)));
        tabNewsTitle.addTab(tabNewsTitle.newTab().setText(list_title.get(1)));
        tabNewsTitle.addTab(tabNewsTitle.newTab().setText(list_title.get(2)));
        tabNewsTitle.addTab(tabNewsTitle.newTab().setText(list_title.get(3)));
        tabNewsTitle.addTab(tabNewsTitle.newTab().setText(list_title.get(4)));
        tabNewsTitle.addTab(tabNewsTitle.newTab().setText(list_title.get(5)));
        tabNewsTitle.addTab(tabNewsTitle.newTab().setText(list_title.get(6)));
        newsTabAdapter = new NewsTabAdapter(getActivity().getSupportFragmentManager(), list_fragment, list_title);
        vpNewsPager.setAdapter(newsTabAdapter);
        tabNewsTitle.setupWithViewPager(vpNewsPager);
        vpNewsPager.setOnPageChangeListener(this);
        ivLeft.setVisibility(View.GONE);


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_learn;
    }

    @Override
    protected void initData() {


    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {

    }


    @OnClick({R.id.iv_left, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left:
                vpNewsPager.setCurrentItem(vpNewsPager.getCurrentItem()-1,true);
                break;
            case R.id.iv_right:
                vpNewsPager.setCurrentItem(vpNewsPager.getCurrentItem()+1,true);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position==0){
            ivLeft.setVisibility(View.GONE);
        }else if (position==list_fragment.size()-1){
            ivRight.setVisibility(View.GONE);
        }else {
            ivLeft.setVisibility(View.VISIBLE);
            ivRight.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Subscribe()
    public void onEventMainThread(NagvationToLearn nagvationToLearn) {
        vpNewsPager.setCurrentItem(nagvationToLearn.getLeadnPosition(),true);
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }
}
